import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { BidPurInvoicesApi } from "@/api/erp/bid/drugProContract";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
import { BidReasonApi } from "@/api/erp/bid/mainReason";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { SupplierApi } from "@/api/erp/purchase/supplier";
let reload: Function;
let id: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.bid";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.name`),
			field: "name",
			component: "Input"
		},
		{
			label: t(`${en_vi}.reasonId`),
			field: "reasonId",
			component: "ApiSelect",
			componentProps: {
				api: BidReasonApi.getBidReasonPage,
				labelField: "name",
				valueField: "id",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.year`),
			field: "year",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.contractDate`),
			field: "contractDate",
			component: "DatePicker"
		},
		{
			label: t("drugStorage.inventoryManagement.drugInventory.warehouseName"),
			field: "whsId",
			component: "ApiSelect",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList1,
				labelField: "whsName",
				valueField: "whsId"
			}
		},
		{
			label: t(`${en_vi}.startDate`),
			field: "startDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.endDate`),
			field: "endDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.qty`),
			field: "qty",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.unitPrice`),
			field: "unitPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.totalAmount`),
			field: "totalAmount",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.suplerName`),
			field: "suplerId",
			component: "ApiSelect",
			componentProps: {
				api: SupplierApi.getSupplierSimpleList,
				labelField: "name",
				valueField: "suplerId"
				// immediate: false
			}
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t("felix_ui.edit") : t("felix_ui.add");
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		id = data.record?.id;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.contractDate = new Date(formValue.contractDate).getTime();
		formValue.startDate = new Date(formValue.startDate).getTime();
		formValue.endDate = new Date(formValue.endDate).getTime();
		let res;
		if (isUpdated) {
			formValue.id = id;
			res = await BidPurInvoicesApi.updateBidPurInvoices(formValue);
		} else {
			res = await BidPurInvoicesApi.createBidPurInvoices(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};

	return (
		<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
